package com.rlyy.common.utils;

import com.rlyy.common.enums.ExceptionEnum;
import com.rlyy.common.exception.BusinessException;
import org.springframework.lang.Nullable;

import java.util.Collection;
import java.util.Objects;

/**
 * @author yuanqinglong
 * @since 2020/7/10 15:54
 */
public abstract class BaseAssert {



    /**
     * 对象不可为空
     *
     * @param object        判断对象
     * @param exceptionEnum 错误信息枚举
     */
    public static void notNull(@Nullable Object object, ExceptionEnum exceptionEnum) {
        if (object == null) {
            throw new BusinessException(exceptionEnum);
        }
    }


    /**
     * 对象不为null抛异常
     *
     * @param object        对象
     * @param exceptionEnum 错误信息
     */
    public static void isNull(@Nullable Object object, ExceptionEnum exceptionEnum) {
        if (object != null) {
            throw new BusinessException(exceptionEnum);
        }
    }


    /**
     * 对象不为null抛异常
     *
     * @param object  对象
     * @param message 错误信息
     */
    public static void isNull(@Nullable Object object, String message) {
        if (object != null) {
            throw new BusinessException(message);
        }
    }


    /**
     * 对象不可为空
     *
     * @param object  判断对象
     * @param message 错误信息
     */
    public static void notNull(@Nullable Object object, String message) {
        if (object == null) {
            throw new BusinessException(message);
        }
    }


    /**
     * 比较equals相等
     *
     * @param e1            对象1
     * @param e2            对象2
     * @param exceptionEnum 错误信息
     */
    public static void equals(@Nullable Object e1, @Nullable Object e2, ExceptionEnum exceptionEnum) {
        if (!Objects.equals(e1, e2)) {
            throw new BusinessException(exceptionEnum);
        }
    }


    /**
     * 比较equals不相等
     *
     * @param e1            对象1
     * @param e2            对象2
     * @param exceptionEnum 错误信息
     */
    public static void nonEquals(@Nullable Object e1, @Nullable Object e2, ExceptionEnum exceptionEnum) {
        if (Objects.equals(e1, e2)) {
            throw new BusinessException(exceptionEnum);
        }
    }


    /**
     * expression为false 抛出异常
     *
     * @param expression    true or false
     * @param exceptionEnum 错误信息
     */
    public static void isTrue(boolean expression, ExceptionEnum exceptionEnum) {
        if (!expression) {
            throw new BusinessException(exceptionEnum);
        }
    }

    /**
     * expression为true 抛出异常
     *
     * @param expression    true or false
     * @param exceptionEnum 错误信息
     */
    public static void isFalse(boolean expression, ExceptionEnum exceptionEnum) {
        if (expression) {
            throw new BusinessException(exceptionEnum);
        }
    }

    /**
     * expression为false 抛出异常
     *
     * @param expression true or false
     * @param message    错误信息
     */
    public static void isTrue(boolean expression, String message) {
        if (!expression) {
            throw new BusinessException(message);
        }
    }


    /**
     * 比较equals
     *
     * @param e1      对象1
     * @param e2      对象2
     * @param message 错误信息
     */
    public static void equals(@Nullable Object e1, @Nullable Object e2, String message) {
        if (!Objects.equals(e1, e2)) {
            throw new BusinessException(message);
        }
    }




    /**
     * 满足条件抛出对应异常
     *
     * @param boole         判断条件
     * @param exceptionEnum 异常信息
     */
    public static void requirement(boolean boole, ExceptionEnum exceptionEnum) {
        if (boole) {
            throw new BusinessException(exceptionEnum);
        }
    }

    /**
     * 集合不包含null元素
     *
     * @param collection 集合
     * @param message    错误信息
     */
    public static void noNullElements(@Nullable Collection<?> collection, String message) {
        if (collection != null) {
            for (Object element : collection) {
                if (element == null) {
                    throw new BusinessException(message);
                }
            }
        }
    }
}
